#include "sys.h"
#include "usart.h"
#include "delay.h"
#include "DigitalOut.h"
#include "events.h"
#include "Timer.h"

#define EVENTSLEN 32
static TEvent Events[EVENTSLEN];
static u8 head = 0, tail = 0;
static u8 empty = 1;
static u8 KeysPollStartFlag;
static u8 KeysRestartTime;
static u8 LastKeyStatus;
static u8  KeyTime[5];

/* destination buffer */
/* source buffer */
void *Hal_smemcpy (void *dest, void *src, int len)
{
	u8 *pd,*ps;
	pd = (u8 *)dest;
	ps = (u8 *)src;
	while(len --)
	{
		*pd = *ps;
		pd ++;
		ps ++;
	}
	return dest;
}

void SetEvent(TEvent *event)
{
  Hal_smemcpy(&Events[tail++ & (EVENTSLEN-1)],event,sizeof(TEvent));
  empty = 0;
  
  return ;
}

TEvent *GetEvent(void)
{
	u8 r;
	if(empty)return NULL;
	r = head;
	head ++;
	if(head == tail) empty = 1;
	return &Events[r & (EVENTSLEN-1)];
}

u8	Update_Key_Status( u8 KeyValue)
{

	TEvent e;

	u8 TicksNow;
	u8 TempKeyStatus;
	u8 TempTime[5];

	TicksNow = Timer2_Get8Seconds();

	if(!KeysPollStartFlag)
	{
		TicksNow -= KeysRestartTime;
		if(TicksNow < 3)
		{
			return   1;
		}
		else
		{
			KeysPollStartFlag  = 1;
		}
	}

	if(KeysPollStartFlag  == 1)
	{
		TempKeyStatus =  KeyValue;
		TempKeyStatus &= 0x1f;

		if((TempKeyStatus&0x01)&&(LastKeyStatus&0x01))
		{
			TempTime[Key_Down] = TicksNow -  KeyTime[Key_Down];

			if(TempTime[Key_Down] > 1)
			{
				{
					NOTIFY_KEY_EVENT(e,Key_Down,keyRepeatEvent);  
				}
			}
		}
		else  if(TempKeyStatus&0x01)
		{
			KeyTime[Key_Down] = TicksNow;
			NOTIFY_KEY_EVENT(e,Key_Down,keyUpEvent);                       
		}
		else
		{
			KeyTime[Key_Down] = TicksNow;
		}

		if((TempKeyStatus&0x02)&&(LastKeyStatus&0x02))
		{
			TempTime[Key_Up] = TicksNow -  KeyTime[Key_Up];

			if(TempTime[Key_Up] > 1)
			{
				{
					NOTIFY_KEY_EVENT(e,Key_Up,keyRepeatEvent);  
				}  
			}
		}
		else if(((TempKeyStatus&0x02)==0)&&(LastKeyStatus&0x02))
		{
			NOTIFY_KEY_EVENT(e,Key_Up,keyDownEvent);  
		}
		else  if(TempKeyStatus&0x02)
		{
			KeyTime[Key_Up] = TicksNow;
			NOTIFY_KEY_EVENT(e,Key_Up,keyUpEvent);                       
		}
		else
		{
			KeyTime[Key_Up] = TicksNow;
		}

		if((TempKeyStatus&0x04)&&(LastKeyStatus&0x04))
		{
			TempTime[Key_DSwitch] = TicksNow -  KeyTime[Key_DSwitch];

			if(TempTime[Key_DSwitch] > 1)
			{
				if(TempTime[Key_DSwitch] > 2)
				{
					NOTIFY_KEY_EVENT(e,Key_DSwitch,keyRepeatLongEvent);  
				}
				else
				{
					NOTIFY_KEY_EVENT(e,Key_DSwitch,keyRepeatEvent);  
				}  
			}
		}
		else if(((TempKeyStatus&0x04)==0)&&(LastKeyStatus&0x04))
		{
			NOTIFY_KEY_EVENT(e,Key_DSwitch,keyDownEvent);  
		}


		else  if(TempKeyStatus&0x04)
		{
			KeyTime[Key_DSwitch] = TicksNow;
			NOTIFY_KEY_EVENT(e,Key_DSwitch,keyUpEvent);                       
		}
		
		else
		{
			KeyTime[Key_DSwitch] = TicksNow;
		}

		if((TempKeyStatus&0x08)&&(LastKeyStatus&0x08))
		{
			TempTime[Key_Set] = TicksNow -  KeyTime[Key_Set];

			if(TempTime[Key_Set] > 1)
			{
				if(TempTime[Key_Set] > 2)
				{
					NOTIFY_KEY_EVENT(e,Key_Set,keyRepeatLongEvent);  
				}
				else
				{
					NOTIFY_KEY_EVENT(e,Key_Set,keyRepeatEvent);  

				}   
			}
		}
		
		else  if(TempKeyStatus&0x08)
		{
			KeyTime[Key_Set] = TicksNow;
			NOTIFY_KEY_EVENT(e,Key_Set,keyUpEvent);                       
		}
		else
		{
			KeyTime[Key_Set] = TicksNow;
		}                    

		if((TempKeyStatus&0x10)&&(LastKeyStatus&0x10))
		{
			TempTime[Key_CSwitch] = TicksNow -  KeyTime[Key_CSwitch];

			if(TempTime[Key_CSwitch] > 3)
			{
				if(TempTime[Key_CSwitch] > 6)
				{
					NOTIFY_KEY_EVENT(e,Key_CSwitch,keyRepeatLongEvent);  
				}
				else
				{
					NOTIFY_KEY_EVENT(e,Key_CSwitch,keyRepeatEvent);  
					KeyTime[Key_CSwitch] = TicksNow;
				}  
			}
		}
		else if(((TempKeyStatus&0x10)==0)&&(LastKeyStatus&0x10))
		{
			NOTIFY_KEY_EVENT(e,Key_CSwitch,keyDownEvent);  
		}
		else  if(TempKeyStatus&0x10)
		{          
			KeyTime[Key_CSwitch] = TicksNow;
			NOTIFY_KEY_EVENT(e,Key_CSwitch,keyUpEvent);                       
		}
		else
		{
			KeyTime[Key_CSwitch] = TicksNow;
		}                         

		LastKeyStatus = TempKeyStatus;
	}
	
	return (u8)0;
}


